package com.acce.gxsc.page.registerCar.addCarInfo;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;

import com.acce.gxsc.R;
import com.acce.gxsc.common.Tools;
import com.acce.sdk.base.mvp.BaseMvpActivity;
import com.acce.sdk.utils.StringUtils;
import com.acce.sdk.widget.EditWithTitle;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by huxley on 2017/5/26.
 */

public class AddCarInfoActivity extends BaseMvpActivity<AddCarInfoView, AddCarInfoPresenter> implements AddCarInfoView {

    @Bind(R.id.ewt_car_no_1)
    EditWithTitle mEwtCarNo1;
    @Bind(R.id.ewt_car_no_2)
    EditWithTitle mEwtCarNo2;
    @Bind(R.id.ewt_hang_no)
    EditWithTitle mEwtHangNo;

    @Override
    protected AddCarInfoPresenter createPresenter() {
        return new AddCarInfoPresenter();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_car_info);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        Intent intent = getIntent();
        if (intent == null) {
            return;
        }
        if (intent.hasExtra("plateNumX")) {
            mEwtCarNo1.setContent(intent.getStringExtra("plateNumX"));
        }
        if (intent.hasExtra("plateNumG")) {
            mEwtCarNo2.setContent(intent.getStringExtra("plateNumG"));
        }
        if (intent.hasExtra("carHangNo")) {
            mEwtHangNo.setContent(intent.getStringExtra("carHangNo"));
        }
    }

    @OnClick(R.id.btn_save)
    public void onClick() {

        String carNo1 = mEwtCarNo1.getContent();
        String carNo2 = mEwtCarNo2.getContent();
        String hangNo = mEwtHangNo.getContent();
        mPresenter.getTime(carNo2);
//        if (StringUtils.isEmpty(carNo1) || StringUtils.isEmpty(carNo2)) {
//            ToastUtils.show("至少填写一个车牌信息！");
//            return;
//        }
    }

    @Override
    public void getTimeSuccess(String time) {
        String message;
        if (StringUtils.isEmpty(time)) {
            message = "该车辆第一次来到本市场";
        } else {
            message = "该车辆已在 " + Tools.getStayTime(time) + " 前录入";
        }
        final AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
        final AlertDialog alertDialog1 = builder1
                .setTitle("提示")
                .setMessage(message)
                .setNegativeButton("取消", null)
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        String carNo1 = mEwtCarNo1.getContent();
                        String carNo2 = mEwtCarNo2.getContent();
                        String hangNo = mEwtHangNo.getContent();
                        Intent intent = new Intent();
                        intent.putExtra("plateNumX", carNo1);
                        intent.putExtra("plateNumG", carNo2);
                        intent.putExtra("carHangNo", hangNo);
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                }).create();
        alertDialog1.setCanceledOnTouchOutside(false);
        alertDialog1.show();

    }
}
